package faf.business.tests;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;

import faf.business.SurveyManager;
import faf.dao.JpaUtils;
import faf.entities.Account;
import faf.entities.Question;
import faf.entities.Sheet;
import faf.entities.Survey;

public class SurveyManagerTests {

	static JpaUtils jpautils;
	static SurveyManager manager;

	@BeforeClass
	public static void beforeClass() {
		HashMap<String, String> config = new HashMap<String, String>();
		config.put("hibernate.hbm2ddl.auto", "create-drop");
		jpautils = new JpaUtils(config);
		manager = new SurveyManager();
		manager.setJpautils(jpautils);
	}
	
	@Test
	public void testCreateSurvey() {
		Survey survey = new Survey();
		
		manager.createSurvey(survey);
		
		Assert.assertNotNull(survey.getId());
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		survey = em.merge(survey);
		em.remove(survey);
		em.getTransaction().commit();
		em.close();
	}
	
	@Test
	public void testFindListQuestion() {
		Survey survey = new Survey();
		List<Question> questions = new LinkedList<Question>();
		Question q0 = new Question();
		q0.setSurvey(survey);
		questions.add(q0);
		Question q1 = new Question();
		q1.setSurvey(survey);
		questions.add(q1);
		
		survey.setQuestionList(questions);
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(survey);
		em.getTransaction().commit();
		
		
		questions = manager.findListQuestion(survey.getId());
		
		Assert.assertEquals(2, questions.size());
		
		em.getTransaction().begin();
		em.remove(survey);
		em.getTransaction().commit();
		
		em.close();
	}
	
	@Test
	public void testModifySurvey() {
		Survey survey = new Survey();
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(survey);
		em.getTransaction().commit();
		em.close();
		
		survey.setOpen(true);
		
		manager.modifySurvey(survey);
		
		em = jpautils.createEntityManger();
		survey = em.find(Survey.class, survey.getId());
		Assert.assertEquals(true, survey.isOpen());
		
		em.getTransaction().begin();
		em.remove(survey);
		em.getTransaction().commit();
		em.close();
	}
	
	@Test
	public void testDeleteSurvey() {
		Survey survey = new Survey();
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(survey);
		em.getTransaction().commit();
		em.close();
		
		manager.deleteSurvey(survey.getId());
		
		em = jpautils.createEntityManger();
		survey = em.find(Survey.class, survey.getId());
		Assert.assertNull(survey);
		em.close();
	}
	
	@Test
	public void testGet() {
		Survey survey = new Survey();
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(survey);
		em.getTransaction().commit();
		
		Survey survey_copy = manager.get(survey.getId());
		
		Assert.assertEquals(survey.getId(), survey_copy.getId());
		
		em.getTransaction().begin();
		em.remove(survey);
		em.getTransaction().commit();
		em.close();
	}
	
	@Test
	public void testGetEmpty() {
		Survey survey = manager.get(123);
		
		Assert.assertNull(survey);
	}
	
	@Test
	public void testListSurveys() {
		Survey survey0 = new Survey();
		Survey survey1 = new Survey();
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(survey0);
		em.persist(survey1);
		em.getTransaction().commit();
		
		Assert.assertEquals(2, manager.listSurveys().size());
		
		em.getTransaction().begin();
		em.remove(survey0);
		em.remove(survey1);
		em.getTransaction().commit();
		
		em.close();
	}
	
	@Test
	public void testIsSurveyTargetTrue() {
		Survey survey = new Survey();
		
		Sheet sheet = new Sheet();
		Account account = new Account();
		sheet.setAccount(account);
		sheet.setSurvey(survey);
		List<Sheet> sheets = new LinkedList<Sheet>();
		sheets.add(sheet);
		survey.setSheetList(sheets);
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(account);
		em.persist(survey);
		em.getTransaction().commit();
		
		Assert.assertTrue(manager.isSurveyTarget(survey, account));
		
		em.getTransaction().begin();
		em.remove(survey);
		em.remove(account);
		em.getTransaction().commit();
	}
	
	@Test
	public void testIsSurveyTargetFalse() {
		Survey survey = new Survey();
		
		Sheet sheet0 = new Sheet();
		Account account0 = new Account();
		sheet0.setAccount(account0);
		sheet0.setSurvey(survey);
		List<Sheet> sheets = new LinkedList<Sheet>();
		sheets.add(sheet0);
		survey.setSheetList(sheets);
		
		Account account1 = new Account();
		
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(account0);
		em.persist(account1);
		em.persist(survey);
		em.getTransaction().commit();
		
		Assert.assertFalse(manager.isSurveyTarget(survey, account1));
		
		em.getTransaction().begin();
		em.remove(survey);
		em.remove(account0);
		em.remove(account1);
		em.getTransaction().commit();
	}
}
